package ui;

import service.LibraryService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * 图书馆管理系统的图形用户界面。
 */
public class LibraryGUI extends JFrame {
    private final LibraryService service;

    /**
     * 构造方法，初始化主窗口。
     * @param service 业务服务
     */
    public LibraryGUI(LibraryService service) {
        this.service = service;
        initializeMainUI();
    }

    /**
     * 初始化主窗口，包含帮助信息和功能按钮。
     */
    private void initializeMainUI() {
        setTitle("图书馆管理系统");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout(10, 10));
        setLocationRelativeTo(null); // 居中显示

        // 添加帮助信息区域（顶部）
        JTextArea helpArea = new JTextArea(
                "欢迎使用图书馆管理系统！\n\n" +
                        "1. [借阅资源]：输入资源ID和借阅者ID，完成资源借阅。\n" +
                        "2. [归还资源]：输入资源ID和借阅者ID，完成资源归还。\n" +
                        "3. [查询资源]：输入资源ID，查看资源详细信息。\n" +
                        "4. [查询借阅者]：输入借阅者ID，查看借阅者信息。\n" +
                        "5. [列出所有资源]：查看所有资源列表。\n" +
                        "6. [列出所有借阅者]：查看所有借阅者列表。"
        );
        helpArea.setEditable(false);
        helpArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        helpArea.setLineWrap(true);
        helpArea.setWrapStyleWord(true);
        helpArea.setBackground(new Color(245, 245, 245));
        JScrollPane helpScrollPane = new JScrollPane(helpArea);

        // 添加功能按钮（中部）
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(6, 1, 10, 10));
        JButton[] buttons = {
                new JButton("借阅资源"),
                new JButton("归还资源"),
                new JButton("查询资源"),
                new JButton("查询借阅者"),
                new JButton("列出所有资源"),
                new JButton("列出所有借阅者")
        };

        for (JButton button : buttons) {
            button.setFont(new Font("微软雅黑", Font.BOLD, 16));
            button.addActionListener(this::handleButtonAction);
            buttonPanel.add(button);
        }

        // 组合布局
        add(helpScrollPane, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.CENTER);

        setVisible(true);
    }

    /**
     * 处理按钮点击事件，打开对应功能弹窗。
     * @param e 点击事件
     */
    private void handleButtonAction(ActionEvent e) {
        String action = e.getActionCommand();

        switch (action) {
            case "借阅资源":
                openBorrowDialog();
                break;
            case "归还资源":
                openReturnDialog();
                break;
            case "查询资源":
                openQueryResourceDialog();
                break;
            case "查询借阅者":
                openQueryBorrowerDialog();
                break;
            case "列出所有资源":
                openListResourcesDialog();
                break;
            case "列出所有借阅者":
                openListBorrowersDialog();
                break;
        }
    }

    /**
     * 打开「借阅资源」弹窗。
     */
    private void openBorrowDialog() {
        JDialog dialog = new JDialog(this, "借阅资源", true);
        dialog.setLayout(new GridLayout(4, 2, 10, 10));
        dialog.setSize(400, 200);
        dialog.setLocationRelativeTo(this);

        JTextField resourceIdField = new JTextField();
        JTextField borrowerIdField = new JTextField();
        JTextArea resultArea = new JTextArea(2, 30);
        resultArea.setEditable(false);

        JButton borrowButton = new JButton("借阅");
        borrowButton.addActionListener(e -> {
            String resourceId = resourceIdField.getText();
            String borrowerId = borrowerIdField.getText();
            String result = service.borrowResource(resourceId, borrowerId);
            resultArea.setText(result);
        });

        dialog.add(new JLabel("资源ID:"));
        dialog.add(resourceIdField);
        dialog.add(new JLabel("借阅者ID:"));
        dialog.add(borrowerIdField);
        dialog.add(borrowButton);
        dialog.add(new JLabel());
        dialog.add(new JScrollPane(resultArea));

        dialog.setVisible(true);
    }

    /**
     *
     */
    private void openReturnDialog() {
        JDialog dialog = new JDialog(this, "归还资源", true);
        dialog.setLayout(new GridLayout(4, 2, 10, 10));
        dialog.setSize(400, 200);
        dialog.setLocationRelativeTo(this);

        JTextField resourceIdField = new JTextField();
        JTextField borrowerIdField = new JTextField();
        JTextArea resultArea = new JTextArea(2, 30);
        resultArea.setEditable(false);

        JButton returnButton = new JButton("归还");
        returnButton.addActionListener(e -> {
            String resourceId = resourceIdField.getText();
            String borrowerId = borrowerIdField.getText();
            String result = service.returnResource(resourceId, borrowerId);
            resultArea.setText(result);
        });

        dialog.add(new JLabel("资源ID:"));
        dialog.add(resourceIdField);
        dialog.add(new JLabel("借阅者ID:"));
        dialog.add(borrowerIdField);
        dialog.add(returnButton);
        dialog.add(new JLabel());
        dialog.add(new JScrollPane(resultArea));

        dialog.setVisible(true);
    }

    /**
     * 打开「查询资源」弹窗。打开「归还资源」弹窗。
     */
    private void openQueryResourceDialog() {
        JDialog dialog = new JDialog(this, "查询资源", true);
        dialog.setLayout(new GridLayout(3, 2, 10, 10));
        dialog.setSize(400, 150);
        dialog.setLocationRelativeTo(this);

        JTextField resourceIdField = new JTextField();
        JTextArea resultArea = new JTextArea(3, 30);
        resultArea.setEditable(false);

        JButton queryButton = new JButton("查询");
        queryButton.addActionListener(e -> {
            String resourceId = resourceIdField.getText();
            String result = service.queryResource(resourceId);
            resultArea.setText(result);
        });

        dialog.add(new JLabel("资源ID:"));
        dialog.add(resourceIdField);
        dialog.add(queryButton);
        dialog.add(new JLabel());
        dialog.add(new JScrollPane(resultArea));

        dialog.setVisible(true);
    }

    /**
     * 打开「查询借阅者」弹窗。
     */
    private void openQueryBorrowerDialog() {
        JDialog dialog = new JDialog(this, "查询借阅者", true);
        dialog.setLayout(new GridLayout(3, 2, 10, 10));
        dialog.setSize(400, 150);
        dialog.setLocationRelativeTo(this);

        JTextField borrowerIdField = new JTextField();
        JTextArea resultArea = new JTextArea(3, 30);
        resultArea.setEditable(false);

        JButton queryButton = new JButton("查询");
        queryButton.addActionListener(e -> {
            String borrowerId = borrowerIdField.getText();
            String result = service.queryBorrower(borrowerId);
            resultArea.setText(result);
        });

        dialog.add(new JLabel("借阅者ID:"));
        dialog.add(borrowerIdField);
        dialog.add(queryButton);
        dialog.add(new JLabel());
        dialog.add(new JScrollPane(resultArea));

        dialog.setVisible(true);
    }

    /**
     * 打开「列出所有资源」弹窗。
     */
    private void openListResourcesDialog() {
        JDialog dialog = new JDialog(this, "资源列表", true);
        dialog.setLayout(new BorderLayout());
        dialog.setSize(600, 400);
        dialog.setLocationRelativeTo(this);

        JTextArea listArea = new JTextArea(15, 40);
        listArea.setEditable(false);
        String listResult = service.listAllResources();
        listArea.setText(listResult);

        JButton closeButton = new JButton("关闭");
        closeButton.addActionListener(e -> dialog.dispose());

        dialog.add(new JScrollPane(listArea), BorderLayout.CENTER);
        dialog.add(closeButton, BorderLayout.SOUTH);

        dialog.setVisible(true);
    }

    /**
     * 打开「列出所有借阅者」弹窗。
     */
    private void openListBorrowersDialog() {
        JDialog dialog = new JDialog(this, "借阅者列表", true);
        dialog.setLayout(new BorderLayout());
        dialog.setSize(600, 400);
        dialog.setLocationRelativeTo(this);

        JTextArea listArea = new JTextArea(15, 40);
        listArea.setEditable(false);
        String listResult = service.listAllBorrowers();
        listArea.setText(listResult);

        JButton closeButton = new JButton("关闭");
        closeButton.addActionListener(e -> dialog.dispose());

        dialog.add(new JScrollPane(listArea), BorderLayout.CENTER);
        dialog.add(closeButton, BorderLayout.SOUTH);

        dialog.setVisible(true);
    }


}
